<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/18
 * Time: 14:33
 */
namespace App\Http\Admin\Product\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Admin\Product as EloquentProduct;

class Product extends Model
{
    public function getProList($where)
    {

        return EloquentProduct::where($where)->toSql();
    }

    /**
     * @param $id 产品id
     * 获取产品信息
     */
    public function getProductInfoById($id){
        $ProductInfo = EloquentProduct::where('id',$id)->first();
        return $ProductInfo;
    }

    public function getProductInfoByNo($productNo){
        $ProductInfo = EloquentProduct::where('product_no', $productNo)->first();
        return $ProductInfo;
    }


    public function addProduct($saveData)
    {
        $ProductInfo = EloquentProduct::firstOrNew(['product_no' => $saveData['product_no']]);
        if ($ProductInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        if($this->saveProduct($ProductInfo, $saveData)){
            return $ProductInfo;
        }else{
            return false;
        }
    }

    public function updateProduct($ProductId, $saveData)
    {
        $ProductInfo = EloquentProduct::find($ProductId);
        if (!$ProductInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->saveProduct($ProductInfo, $saveData);
    }

    private function saveProduct(EloquentProduct $ProductInfo, array $saveData)
    {
        $ProductInfo->fill($saveData);
        return $ProductInfo->save();
    }
}